//
//  SAAnswerQuestionsViewController.m
//  SunOn
//
//  Created by RazeenCheng on 2017/5/2.
//  Copyright © 2017年 NeoZhang. All rights reserved.
//

#import "SAAnswerQuestionsViewController.h"
#import "SAAnswerQuestionCollectionViewCell.h"
#import "SAQuestionnaireAnalysisViewController.h"

@interface SAAnswerQuestionsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UILabel *questionTitleLabel; // 调查问卷标题
@property (weak, nonatomic) IBOutlet UILabel *currentIndexLabel; // 当前的进度
@property (weak, nonatomic) IBOutlet UILabel *questionCountLabel; // 调查问卷总题数

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UIButton *previousButton; // 上一题
@property (weak, nonatomic) IBOutlet UIButton *nextButton; // 下一题

@property (nonatomic, assign) NSInteger currentIndex; // 当前答题进度
@property (nonatomic, strong) NSMutableArray *answersArray; // 记录用户填写问卷的选项

@property (nonatomic, strong) NSMutableArray *questionArray;

@end

@implementation SAAnswerQuestionsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"问卷调查";
    
    [self prepareForNavigation];
    
    self.previousButton.layer.borderColor = [[UIColor colorWithHexString:@"0xE1E1E1"] CGColor];
    self.previousButton.layer.borderWidth = 1;
    
    for (NSInteger i = 0; i < self.questionArray.count; i++) {
        NSMutableArray *selctedItemArray = [NSMutableArray array];
        [self.answersArray addObject:selctedItemArray];
    }
    
    [self prepareForCollectionView];
    
    self.currentIndex = 1;
    self.questionCountLabel.text = [NSString stringWithFormat:@"/ %ld",(long)self.questionArray.count];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation
- (void)prepareForNavigation
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 20);
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithHexString:@"000000"] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

- (void)rightButtonAction
{
    if ([self checkAllQuestionIsAnswer]) {
        // 已经回答了所有的问题
        SAQuestionnaireAnalysisViewController *questionnaireAnalysisVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SAQuestionnaireAnalysisViewController"];
        questionnaireAnalysisVC.answerString = [self getAllAnswer];
        [self.navigationController pushViewController:questionnaireAnalysisVC animated:YES];
    } else {
        // 未回答所有的问题
    }
}


#pragma mark - 
- (void)prepareForCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(SASCREEN_WIDTH, SASCREEN_HEIGHT - 64 - 50 - 65);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.scrollEnabled = NO;
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}


#pragma mark - 懒加载
- (NSMutableArray *)answersArray
{
    if (!_answersArray) {
        _answersArray = [NSMutableArray array];
    }
    return _answersArray;
}

- (NSMutableArray *)questionArray
{
    if (!_questionArray) {
        NSArray *question1 = [NSArray arrayWithObjects:@"1.您的年龄", @"0", @"8岁以下", @"8-22岁", @"23-45岁", @"46-60岁", nil];
        NSArray *question2 = [NSArray arrayWithObjects:@"2.您从事的行业是", @"0", @"IT", @"金融", @"教育", @"服务", @"其他", nil];
        NSArray *question3 = [NSArray arrayWithObjects:@"3.您的月收入是", @"0", @"1000元以下", @"2000-3000元", @"3000-6000元", @"6000元以上", nil];
        NSArray *question4 = [NSArray arrayWithObjects:@"4.您每年用于游泳健身的费用是多少？", @"0", @"100元以下", @"100-300", @"300以上", nil];
        NSArray *question5 = [NSArray arrayWithObjects:@"5.您的泳龄是多久", @"0", @"一个月", @"三个月", @"六个月", @"一年以上", nil];
        NSArray *question6 = [NSArray arrayWithObjects:@"6.您去游泳馆游泳的目的", @"1", @"锻炼身体", @"缓解工作压力", @"跟随潮流", @"社交方式，以此结交更多朋友", @"一种自我展示的方式", nil];
        NSArray *question7 = [NSArray arrayWithObjects:@"7.您选择游泳馆看重的是什么", @"1", @"价格", @"与工作单位或家庭距离的远近", @"规模和档次", @"品牌效应", @"停车是否便利", @"教练、救生的素质和技能", @"服务人员耐心周到的服务态度", nil];
        _questionArray = [NSMutableArray arrayWithObjects:question1, question2, question3, question4, question5, question6, question7, nil];
    }
    return _questionArray;
}


// 上一题
- (IBAction)previousQuestionAction:(id)sender
{
    if (self.currentIndex == 1) {
        return;
    }
    
    self.currentIndex--;
    [self.collectionView setContentOffset:CGPointMake(SASCREEN_WIDTH * (self.currentIndex - 1), 0) animated:YES];
    
    [self refreshCurrentIndex];
}

// 下一题
- (IBAction)nextButtonAction:(id)sender
{
    if (self.currentIndex == self.questionArray.count) {
        return;
    }
    
    [self.collectionView setContentOffset:CGPointMake(SASCREEN_WIDTH * self.currentIndex, 0) animated:YES];
    
    self.currentIndex++;
    [self refreshCurrentIndex];
}

- (void)refreshCurrentIndex
{
    self.currentIndexLabel.text = [NSString stringWithFormat:@"%ld",(long)self.currentIndex];
}



#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.questionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SAAnswerQuestionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAAnswerQuestionCollectionViewCell" forIndexPath:indexPath];
    [cell configCellWithIndex:indexPath.item withArray:self.questionArray[indexPath.row] withSelectItemArray:self.answersArray[indexPath.row]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SASCREEN_WIDTH, SASCREEN_HEIGHT - 64 - 50 - 65);
}


// 判断是否已经回答了所有问题
- (BOOL)checkAllQuestionIsAnswer
{
    for (NSMutableArray *selectItemArray in self.answersArray) {
        if (selectItemArray.count <= 0) {
            return NO;
        }
    }
    return YES;
}

- (NSString *)getAllAnswer
{
    NSString *answerString = @"";
    for (NSInteger i = 0; i < self.answersArray.count; i++) {
        answerString = [answerString stringByAppendingString:[NSString stringWithFormat:@"%ld.",i + 1]];
        NSMutableArray *selectItemArray = self.answersArray[i];
        
        for (NSIndexPath *selectIndexPath in selectItemArray) {
            answerString = [answerString stringByAppendingString:[NSString stringWithFormat:@"%c",(int)(selectIndexPath.row + 65)]];
        }
        
        answerString = [answerString stringByAppendingString:@"\n"];
    }
    
    return answerString;
}


@end
